/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: filewrite.c
> Author:MOKI
> Mail:1913964829@qq.com
> Created Time: 2024年09月13日 星期五 18时50分23秒
***************************************************/
#include "global.h"
void fileWriteEmployee(DataList *head)
{
    DataList *end = head;
    FILE *fp = fopen(EMPLOYEES_FILE_PATH, "w");
    while (end != NULL)
    {
        fwrite(&(end->datatype.employee), sizeof(Employee), 1, fp);
        end = end->pn;
    }
    fclose(fp);
}
void fileWritePlant(DataList *head)
{
    DataList *end = head;
    FILE *fp = fopen(PLANTS_FILE_PATH, "w");
    while (end != NULL)
    {
        fwrite(&(end->datatype.plant), sizeof(Plant), 1, fp);
        end = end->pn;
    }
    fclose(fp);
}
void fileWriteSurveillanceData(DataList *head)
{
    DataList *end = head;
    FILE *fp = fopen(SURVEILLANCE_DATA_FILE_PATH, "w");
    while (end != NULL)
    {
        fwrite(&(end->datatype.surveillanceData), sizeof(SurveillanceData), 1, fp);
        end = end->pn;
    }
    fclose(fp);
}
void fileWriteEnvironmentData(DataList *head)
{
    DataList *end = head;
    FILE *fp = fopen(ENVIRONMENT_DATA_FILE_PATH, "w");
    while (end != NULL)
    {
        fwrite(&(end->datatype.environmentData), sizeof(EnvironmentData), 1, fp);
        end = end->pn;
    }
    fclose(fp);
}
void fileWriteVisitor(DataList *head)
{
    DataList *end = head;
    FILE *fp = fopen(VISITORS_FILE_PATH, "w");
    while (end != NULL)
    {
        fwrite(&(end->datatype.visitor), sizeof(Visitor), 1, fp);
        end = end->pn;
    }
    fclose(fp);
}

void fileWriteGlobal(int num, char name[50])
{
    FILE *fp = fopen(GLOBAL_VARIABLE_FILE_PATH, "w");
    GlobalVariable global = {0};
    if (num == 1)
    {
        strcpy(global.global_name_employee, name);
    }
    else if (num == 2)
    {
        strcpy(global.global_name_visitor, name);
    }
    else
    {
        printf("参数错误");
        fclose(fp);
        return;
    }

    fwrite(&global, sizeof(GlobalVariable), 1, fp);
    fclose(fp);
}
/*
char *fileReadGlobal(int num){
    FILE *fp = fopen(GLOBAL_VARIABLE_FILE_PATH, "r");
    GlobalVariable global = {0};
    fread(&global,sizeof(GlobalVariable),1,fp);
    fclose(fp);
    if (num == 1)
    {
        return global.global_name_employee;
    }
    else if (num == 2)
    {
        return global.global_name_visitor;
    }
    else
    {
        printf("参数错误");
        return;
    }

}
*/